from rest_framework.generics import ListAPIView, CreateAPIView
from users.models import User
from meiduo_admin.serializer.user_serializer import UserSerializer
from meiduo_admin.utils import PageNum


class UserViews(ListAPIView, CreateAPIView):
    """
    获取用户信息
    """
    # 获取查询集
    queryset = User.objects.all()
    # 指定序列化器
    serializer_class = UserSerializer
    # 指定分页器
    pagination_class = PageNum

    # 重写获取查询集数据的方法
    def get_queryset(self):
        if self.request.query_params.get('keyword') == '':
            return User.objects.all()
        else:
            return User.objects.filter(username__contains=self.request.query_params.get('keyword'))
